package com.doctor.adapter;

import java.util.List;

import com.doctor.bean.KnowledgeBean;
import com.doctor.comm.MyApplication;
import com.doctor.ui.R;
import com.doctor.view.BadgeView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class KnowledgeThirdAdapter extends BaseAdapter{

	/**
	 * 医学知识三级目录adapter
	 */
	
	private Context context;
	private List<KnowledgeBean> beanList;
	private ThirdClickCallback callback;
	private Click click;
	//新标志所需长度
	private int newFlagPadding ;
	
	
	public KnowledgeThirdAdapter(Context context, List<KnowledgeBean> beanList, ThirdClickCallback callback){
		this.context=context;
		this.beanList=beanList;
		this.callback=callback;
		click=new Click();
		newFlagPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 22, MyApplication.getInstance().getResources().getDisplayMetrics());
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beanList.size()-1;
	}

	@Override
	public KnowledgeBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return beanList.get(1+arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if (arg1==null) {
			holder=new ViewHolder();
			arg1=View.inflate(context, R.layout.knowledge_third_item, null);
			holder.third=(TextView) arg1.findViewById(R.id.third);
			holder.newFlag = new BadgeView(context, holder.third);
			holder.newFlag.setText("new");
			holder.newFlag.setPadding(0, 0, 0, 0);
			holder.newFlag.setBadgeMargin(0);
			holder.newFlag.setTypeface(Typeface.DEFAULT);
			holder.newFlag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
			holder.newFlag.setTextColor(Color.RED);
			holder.newFlag.setBackgroundColor(Color.TRANSPARENT);
			
			arg1.setTag(holder);
		} else {
			holder=(ViewHolder) arg1.getTag();
		}
		
		KnowledgeBean bean=getItem(arg0);
		
		holder.third.setText(bean.getDirname());
		
		boolean isNew = bean.getIsNew();
		if (isNew) {
			holder.third.setPadding(0, 0, newFlagPadding, 0);
			//打上new标志
			holder.newFlag.show();
		} else {
			holder.newFlag.hide();
			holder.third.setPadding(0, 0, 0, 0);
		}
		
		arg1.setTag(R.id.bean_tag,bean);
		arg1.setOnClickListener(click);
		
		return arg1;
	}
	
	private class ViewHolder{
		TextView third;
		BadgeView newFlag;
	}
	
	private class Click implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			callback.onClick((KnowledgeBean) v.getTag(R.id.bean_tag));
		}
	}
	
	public interface ThirdClickCallback{
		public void onClick(KnowledgeBean bean);
	}

}
